package com.moonypoony.superactionrpg;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;


public class myGLSurfaceView extends GLSurfaceView {
	

	private myRenderer renderer;
	
	public myGLSurfaceView(Context context) {
		super(context);
		setEGLConfigChooser(8, 8, 8, 8, 16, 0); 
		renderer = new myRenderer(context);
		setRenderer(renderer);
		//setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e) {
		
		int touchCount = e.getPointerCount();
		float w = this.getWidth();
		float h = this.getHeight();
		
		for(int i=0; i < touchCount; i++) {
			float x = e.getX(i);
			float y = e.getY(i);
			int id = e.getPointerId(i);
			int action = e.getActionMasked();
			//int actionIndex = e.getActionIndex();
			
			switch (action) {
			case MotionEvent.ACTION_DOWN: {
				touch(x, y, w, h, id);
				
				break;
			}
			
			case MotionEvent.ACTION_POINTER_DOWN: {
				touch(x, y, w, h, id);
				
				break;
			}
			
			case MotionEvent.ACTION_MOVE: {
				touch(x, y, w, h, id);				
				
				break;
			}
			
			case MotionEvent.ACTION_UP: {
				renderer.releaseButton(id);
				break;
			}
			
			case MotionEvent.ACTION_POINTER_UP: {
				renderer.releaseButton(id);
				break;
			}
			}
		}
		return true;
	}
	
	private void touch(float x, float y, float w, float h, int id) {

		if(y/h>0.8875f && x/w<0.13f && x/w>0.0675f) { // down
			renderer.pressDPad(0, id);
		}
		if(y/h<0.8875f && y/h>0.7792f && x/w<0.0675f) { //left
			renderer.pressDPad(1, id);
		}
		if(y/h>0.6667f && y/h<0.7792f && x/w<0.13f && x/w>0.0675f) { //up
			renderer.pressDPad(2, id);
		}
		if(y/h<0.8875f && y/h>0.7792f && x/w<0.2f && x/w>0.13f) { //right
			renderer.pressDPad(3, id);
		}
		if(y/h>0.8667f && x/w>0.92) { //bbutton
			renderer.pressbbutton(id);
		}
		
	}

}
